/*
 * @Description  : 
 * @Author       : zhao
 * @Date         : 2022-06-28 11:16:19
 * @LastEditors  : zhao
 * @LastEditTime : 2023-01-31 14:36:28
 */

import { Message } from 'element-ui'
//获取中心坐标
let getCentralData = (oneLon, oneLat, twoLon, twoLat) => {
  //oneLon：第一个点的经度；oneLat：第一个点的纬度；twoLon：第二个点的经度；twoLat：第二个点的纬度；
  let aLon = 0, aLat = 0;
  let bLon = Number(oneLon) - Number(twoLon);
  let bLat = Number(oneLat) - Number(twoLat);
  //Math.abs()绝对值
  if (bLon > 0) {
    aLon = Number(oneLon) - Math.abs(bLon) / 2;
  } else {
    aLon = Number(twoLon) - Math.abs(bLon) / 2;
  }
  if (bLat > 0) {
    aLat = Number(oneLat) - Math.abs(bLat) / 2;
  } else {
    aLat = Number(twoLat) - Math.abs(bLat) / 2;
  }
  return [aLon, aLat];

}
const state = {
  PaginationData: { //分页参数设置

    total: 0,
    currentPage: 1,
    isbackground: false,
    layout: "total, sizes, prev, pager, next, jumper",
    pagesize: 10,
  },
  tableData: [],
  searchData: {
    pageSize: 10,
    pageIndex: 1,
  }, // 用户输入数据
  // leftSearchData: {},
  // rightSearchData: {},
  url: "",//接口调取名称
  rowData: {},
  selectData: [],
  clearQueryCondition: false,//是否清空查询条件
}
const key = {
  rowData: 'rowData'
}
const mutations = {
  SET_ROWDATA: (state, value) => {
    state.rowData = value
    sessionStorage.setItem(key.rowData, JSON.stringify(value))
  },
  //下载文件流
  getFileFlow(state, data) {
    // // 创建blob对象，解析流数据
    const blob = new Blob([data.res], {
      // 设置返回的文件类型
      // type: 'application/pdf;charset=UTF-8' 表示下载文档为pdf，如果是word则设置为msword，excel为excel
      // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      type: data.type
    })

    // 这里就是创建一个a标签，等下用来模拟点击事件
    const a = document.createElement('a')
    // 兼容webkix浏览器，处理webkit浏览器中href自动添加blob前缀，默认在浏览器打开而不是下载
    const URL = window.URL || window.webkitURL
    // 根据解析后的blob对象创建URL 对象
    const herf = URL.createObjectURL(blob)
    // 下载链接
    a.href = herf
    // 下载文件名,如果后端没有返回，可以自己写a.download = '文件.pdf'
    a.download = data.fileName || "fileName"
    document.body.appendChild(a)
    // 点击a标签，进行下载 
    a.click()
    // 收尾工作，在内存中移除URL 对象
    document.body.removeChild(a)
    window.URL.revokeObjectURL(herf)
  }
}

const actions = {
  async getTableData({ state, dispatch, rootState }) {

    const search = state.searchData

    const url = state.url
    console.log("🚀 ~ file: table.js:108 ~ getTableData ~ state.url", state.url)
    const { data, code, count } = await dispatch(url, search, { root: true })
    if (code == 200) {

      if (state.url == "borrowForm/getBorrowingForm") {
        data.map(e => {


          if (e.state == "1") e.state = "审核中"
          if (e.state == "2") e.state = "已驳回"
          if (e.state == "3") e.state = "已通过"
          if (e.state == "4") e.state = "已完成"
          if (e.state == "5") e.state = "已过期"
          if (e.state == "6") e.state = "已撤销"
        })
      }
      else if (state.url == "map/getResultFromPoints" || state.url == "ArchiveInfo/getArchive") {

        state.PaginationData.total = count
        data.map(e => {
          e.LonLat = getCentralData(e.maxX, e.maxY, e.minX, e.minY)
        })
        rootState.map.mapList = data
      } else {
      }
      state.tableData = data
      state.PaginationData.total = count
    }



  }

}
const getters = {
  GET_ROWDATA: (state) => {
    if (Object.keys(state.rowData).length == 0) {

      state.rowData = JSON.parse(sessionStorage.getItem(key.rowData))
    }

    return state.rowData
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
